// pages/boxProblems/boxProblems.js  
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    que_type: '',
    page_num: 1,
    changedLocationInfo: {},
    provIndex: 0,
    cityIndex: 0,
    hosIndex: 0,
    showPile: true,
    showOptReason: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      que_type: options.type,
      scrollHeight: app.getScrollHeight(105)
    })
    if (that.data.que_type == 'equ') {
      wx.setNavigationBarTitle({
        title: '设备问题统计',
      })
    }
    that.getQueType()
    if (!!options.hospitalId && options.hospitalId != undefined) {
      that.setData({
        hospitalId: options.hospitalId,
        // hospitalName: options.hospitalName,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (that.data.changedLocationInfo.hospitalId) {
      that.setData({
        hospitalId: that.data.changedLocationInfo.hospitalId,
        hospitalName: that.data.changedLocationInfo.hospitalName,
        page_num: 1
      })
    }
    that.getCitys()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getCitys: function () {
    let that = this
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      let provList = ['全部']
      that.data.propArray.forEach(item => {
        provList.push(item.provName)
      })
      that.setData({
        provList: provList
      })
      that.setIdArray()
    })
  },
  provChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      provIndex: index,
      page_num: 1,
      cityIndex: 0
    })
    that.setIdArray()
  },
  cityChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      page_num: 1,
      cityIndex: index
    })
    that.setIdArray()
  },
  hosChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      page_num: 1,
      hosIndex: index
    })
    that.getProblemList(that.data.problemType)
  },
  setCityList: function () {
    let that = this
    let cityList = ['全部']
    if (that.data.provIndex > 0) {
      that.data.propArray[that.data.provIndex - 1].cityList.forEach(item => {
        cityList.push(item.cityName)
      })
    }
    that.setData({
      cityList: cityList
    })
    let hosList = ['全部']
    that.data.hospitalList.forEach(item => {
      hosList.push(item.hospitalName)
    })
    that.setData({
      hosList: hosList
    })
    // that.getProblemList()
  },
  getHospitalList: function () {
    let that = this
    let selectorArray = that.data.selectorArray
    lbbService.getHospitalList({
      array: selectorArray
    }, function (res) {
      that.setData({
        hospitalList: res
      })
      that.setCityList()
      that.getProblemList(that.data.problemType)
      // that.getCounts()
    })
  },
  setIdArray: function () {
    let that = this
    let provId = 0
    if (that.data.provIndex > 0) {
      provId = that.data.propArray[that.data.provIndex - 1].id
    }
    let cityId = 0
    if (that.data.cityIndex > 0) {
      if (that.data.provIndex > 0) {
        cityId = that.data.propArray[that.data.provIndex - 1].cityList[that.data.cityIndex - 1].cityId
      } else {
        cityId = that.data.propArray[that.data.provIndex].cityList[that.data.cityIndex - 1].cityId
      }
    }
    let selectorArray = [provId, cityId, 0]
    that.setData({
      selectorArray: selectorArray
    })
    that.getHospitalList()
  },

  getQueType: function () {
    let that = this
    let que_type = that.data.que_type
    if (que_type == 'box') {
      that.setData({
        problemType: 'no_rfid', //默认显示问题类型
        typeList: [{
          type: 'no_rfid',
          num: 0,
          desc: 'RFID不识别',
          checked: true,
        }, {
          type: 'no_eject',
          num: 0,
          desc: '锁钩弹不开'
        }, {
          type: 'blue_no_con',
          num: 0,
          desc: '蓝牙连接不上'
        }, {
          type: 'insert_switch',
          num: 0,
          desc: '插入开关'
        }, {
          type: 'hook_stat',
          num: 0,
          desc: '锁钩开关'
        },
        //  {
        //   type: 'lock_switch',
        //   num: 0,
        //   desc: '开关故障'
        // }, 
        {
          type: 'repair',
          num: 0,
          desc: '维修中'
        }, {
          type: 'restart',
          num: 0,
          desc: '重启'
        }, {
          type: 'renovation',
          num: 0,
          desc: '自动修复'
        }, {
          type: 'low_battery',
          num: 0,
          desc: '低电'
        }, {
          type: 'mannual_back',
          num: 0,
          desc: '人工归还'
          }, {
          type: 'timeout85',
            num: 0,
            desc: '85超时弹不开'
          }, {
          type: 'hook_err_noeject',
            num: 0,
            desc: '锁钩错误弹不开'
          }
          // , {
          //   type: 'search_fail',
          //   num: 0,
          //   desc: '寻卡失败'
          // }, {
          //   type: '85_error',
          //   num: 0,
          //   desc: '85数据返回错误'
          // },
          //  {
          //   type: 'ble_restart',
          //   num: 0,
          //   desc: '蓝牙连接过程中重启'
          // }, 
          // {
          //   type: 'motor_power_no_split',
          //   num: 0,
          //   desc: '马达供电未分开'
          // }, 
          //  {
          //   type: 'pile_one_minute',
          //   num: 0,
          //   desc: '1分钟租借'
          // }
        ],
      })

    } else {
      that.setData({
        problemType: 'lose_chair', //默认显示问题类型
        problemText: '疑似丢失',
        typeList: [{
          type: 'lose_chair',
          num: 0,
          desc: '疑似丢失',
          checked: true,
        }, {
          type: 'equ_repair',
          num: 0,
          desc: '维护中'
        }, {
          type: 'equ_no_back',
          num: 0,
          desc: '不归还'
        }, {
          type: 'equ_damage_down',
          num: 0,
          desc: '赔偿下线'
        }, {
          type: 'one_week_no_back',
          num: 0,
          desc: '一周未归还'
        }, {
          type: 'other_address_back',
          num: 0,
          desc: '异地归还'
        }, {
          type: 'three_days_no_back',
          num: 0,
          desc: '三天未租借'
          }, {
            type: 'equ_manual_back',
            num: 0,
            desc: '人工归还'
          }],
      })
    }
  },
  inputPile: function (e) {
    let that = this
    console.log('e>>', e)
    let pileCode = e.detail.value.replace(/[^0-9]/ig, "")
    console.log('pileCode', pileCode)
    if (!!pileCode) {
      that.setData({
        pileCode: 'Z' + pileCode
      })
    } else {
      that.setData({
        pilesFilter: that.data.piles
      })
    }
  },
  inputEquip: function (e) {
    let that = this
    console.log('e>>', e)
    let equipCode = e.detail.value.replace(/[^0-9]/ig, "")
    console.log('equipCode', equipCode)
    if (!!equipCode) {
      that.setData({
        equipCode: 'L' + equipCode
      })
    } else {
      that.setData({
        chairsFilter: that.data.chairs
      })
    }
  },
  searchEquip: function () {
    let that = this
    if (that.data.que_type == 'box') {
      let piles = that.data.piles
      piles = piles.filter(item => item.pile_code == that.data.pileCode)
      console.log('piles', piles)
      that.setData({
        pilesFilter: piles
      })
    } else {
      let chairs = that.data.chairs
      chairs = chairs.filter(item => item.chair_code == that.data.equipCode)
      console.log('chairs', chairs)
      that.setData({
        chairsFilter: chairs
      })
    }
  },
  getMore: function () {
    let that = this
    console.log('getMore>> ')
    that.data.page_num += 1
    that.getProblemList(that.data.problemType)
  },
  typeChange: function (e) {
    let that = this
    console.log('e>>', e)
    let index = e.detail.value
    that.setData({
      problemType: that.data.typeList[index].type,
      problemText: that.data.typeList[index].desc,
      page_num: 1
    })
    let problemType=that.data.problemType;
    that.setData({
      showPile : false,
      showNowStatName: true,
      showLeaseUserName: false,
      showEequUseInfo: false,
      showOptUserNname :false,
      showHospitalName:false,
      showOptReason: false
    });
    if (problemType == 'other_address_back') {
      that.setData({
        showHospitalName: true
      });
    }
    if (problemType == 'equ_damage_down') {
      that.setData({
        showOptUserNname: true,
        showLeaseUserName: true
      });
    }
    if (problemType == 'three_days_no_back' ) {
      that.setData({
        showPile: true,
        showEequUseInfo: true
      });
    } 
    if (problemType == 'one_week_no_back' || problemType == 'equ_no_back') {
      that.setData({
        showLeaseUserName: true,
        showEequUseInfo: true
      });
    } 
    if (problemType == 'equ_manual_back' || problemType == 'equ_repair') {
      that.setData({
        showPile: true,
        showOptUserNname: true,
        showOptReason: true
      });
    } 
    if (problemType == 'equ_manual_back') {
      that.setData({
        showPile: true,
        showOptUserNname: true,
        showOptReason: true
      });
    } 
    if (problemType =='lose_chair'){
       that.setData({
         showPile:true,
         showOptReason:true
       }); 
    }    
    let typeList = that.data.typeList
    typeList.forEach(item => {
      item.checked = false
      if (item.type == that.data.problemType) {
        item.checked = true
      }
    })
    that.setData({
      typeList: typeList
    })
    that.getProblemList(that.data.problemType)
  },
  setTypeList: function () {
    let that = this
    let counts = that.data.counts
    let typeList = that.data.typeList
    console.log('setTypeList---->', counts, typeList)
    typeList.forEach(item => {
      Object.keys(counts).forEach(key => {
        if (key == item.type) {
          item.num = parseInt(counts[key])
        } else if (item.num == null || item.num == undefined) {
          item.num = 0
        }
      })
    })
    that.setData({
      typeList: typeList
    })
  },
  getCounts: function () {
    let that = this
    let hospitalId = 0
    if (that.data.hosIndex != 0) {
      hospitalId = that.data.hospitalList[that.data.hosIndex - 1].id
    } else if (that.data.hospitalId) {
      hospitalId = that.data.hospitalId
    }
    let typeList = that.data.typeList
    for (let i = 0; i < typeList.length; i++) {
      lbbService.getSysProblems({
        que_type: that.data.que_type,
        hospitalId: hospitalId,
        err_type: typeList[i].type,
        page_num: that.data.page_num,
        provinceId: that.data.selectorArray[0],
        cityId: that.data.selectorArray[1],
      }, function (res) {
        // that.setBoxCount(res.data.counts, typeList[i].type)
      })
    }

  },
  getProblemList: function (problemType) {
    let that = this
    let hospitalId = 0
    if (that.data.hosIndex != 0) {
      hospitalId = that.data.hospitalList[that.data.hosIndex - 1].id
    } else if (that.data.hospitalId) {
      hospitalId = that.data.hospitalId
    }
    lbbService.getSysProblems({
      que_type: that.data.que_type,
      hospitalId: hospitalId,
      // hospitalId: null,
      err_type: problemType,
      page_num: that.data.page_num,
      provinceId: that.data.selectorArray[0],
      // provinceId: null,
      cityId: that.data.selectorArray[1],
      // cityId: null,
    }, function (res) {
      that.handleRes(res, problemType)
    })
  },
  handleRes: function (res, problemType) {
    let that = this
    if (that.data.que_type == 'box') {
      if (that.data.page_num != 1) {
        that.setData({
          piles: that.data.piles.concat(res.data.piles),
          pilesFilter: that.data.piles.concat(res.data.piles),
        })
      } else {
        console.log('res------>', res)
        that.setData({
          piles: res.data.piles,
          pilesFilter: res.data.piles,
        })
        if (typeof (res.data.counts) == 'object') {
          that.setData({
            counts: res.data.counts,
          })
          that.setTypeList()
        } else if (typeof (res.data.counts) == 'number') {
          that.setData({
            counts: res.data.counts,
          })
          that.setBoxCount(res.data.counts, problemType)
        }
      }
    } else {
      if (that.data.page_num != 1) {
        that.setData({
          chairs: that.data.chairs.concat(res.data.chairs),
          chairsFilter: that.data.chairs.concat(res.data.chairs),
        })
      } else {
        that.setData({
          chairs: res.data.chairs,
          chairsFilter: res.data.chairs,
        })
        if (!!res.data.counts) {
          that.setData({
            counts: res.data.counts,
          })
          that.setTypeList()
        }
      }

    }

  },
  setBoxCount: function (count, type) {
    let that = this
    let typeList = that.data.typeList
    console.log('setBoxCount---->', count, type)
    for (let i = 0; i < typeList.length; i++) {
      if (type == typeList[i].type) {
        typeList[i].num = parseInt(count)
      }
    }
    // that.data.typeList = typeList
    that.setData({
      typeList: typeList
    })
  }
})
